﻿#include "selectmethodpage.h"
#include "ui_selectmethodpage.h"
#include "global_funcs.h"
#include "selectmodulepage.h"
#include <QStringListModel>
#include <QFontMetrics>
#include <QTimer>
#include <QMessageBox>
#include <QDebug>

SelectMethodPage::SelectMethodPage(int selectModulePageID, QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::SelectMethodPage),
    selectModulePageID(selectModulePageID)
{
    ui->setupUi(this);
    modelMethods = new QStringListModel(this);
    ui->lvMethods->setModel(modelMethods);
    ui->lvMethods->setItemDelegate(new MethodDelegate());
    modelSelectedMethods = new QStringListModel(this);
    ui->lvSelectedMethods->setModel(modelSelectedMethods);
    ui->lvSelectedMethods->setItemDelegate(new MethodDelegate());
}

SelectMethodPage::~SelectMethodPage()
{
    delete ui;
}

const QStringList& SelectMethodPage::getSelectedMethods()
{
    return selectedMethods;
}

void SelectMethodPage::initializePage()
{
    SelectModulePage* selectModulePage = qobject_cast<SelectModulePage*>(this->wizard()->page(selectModulePageID));
    Q_ASSERT(selectModulePage != nullptr);

    QStringList methods;
    if (!enumMethod(selectModulePage->getSelectedModuleName(), methods)) {
        qCritical() << "enumMethod failed!";
        QMessageBox::critical(this, windowTitle(), QStringLiteral("enumMethod failed!"));
        QTimer::singleShot(0, wizard(), SLOT(back()));
        return;
    }

    modelMethods->setStringList(methods);
    ui->lvMethods->setCurrentIndex(modelMethods->index(-1));
}

bool SelectMethodPage::isComplete() const
{
    return selectedMethods.size() > 0;
}

QSize SelectMethodPage::sizeHint() const
{
    return QSize(800, 600);
}

void SelectMethodPage::on_pbAdd_clicked()
{
    QModelIndex index = ui->lvMethods->currentIndex();
    if (index.isValid()) {
        QString selectedMethod = modelMethods->data(index).toString();
        selectedMethods.append(selectedMethod);
        modelSelectedMethods->setStringList(selectedMethods);
        emit completeChanged();
    }
}

void SelectMethodPage::on_pbRemove_clicked()
{
    QModelIndex index = ui->lvSelectedMethods->currentIndex();
    if (index.isValid()) {
        selectedMethods.removeAt(index.row());
        //QString selectedMethod = modelSelectedMethods->data(index).toString();
        //selectedMethods.removeOne(selectedMethod);
        modelSelectedMethods->setStringList(selectedMethods);
        emit completeChanged();
    }
}

void SelectMethodPage::on_pbUp_clicked()
{
    QModelIndex index = ui->lvSelectedMethods->currentIndex();
    if (index.isValid()) {
        if (index.row() > 0) {
            QString selectedMethod = selectedMethods.takeAt(index.row());
            selectedMethods.insert(index.row() - 1, selectedMethod);
            modelSelectedMethods->setStringList(selectedMethods);
            ui->lvSelectedMethods->setCurrentIndex(modelSelectedMethods->index(index.row() - 1, 0));
        }
    }
}

void SelectMethodPage::on_pbDown_clicked()
{
    QModelIndex index = ui->lvSelectedMethods->currentIndex();
    if (index.isValid()) {
        if (index.row() < modelSelectedMethods->rowCount() - 1) {
            QString selectedMethod = selectedMethods.takeAt(index.row());
            selectedMethods.insert(index.row() + 1, selectedMethod);
            modelSelectedMethods->setStringList(selectedMethods);
            ui->lvSelectedMethods->setCurrentIndex(modelSelectedMethods->index(index.row() + 1, 0));
        }
    }
}

void SelectMethodPage::on_lvMethods_doubleClicked(const QModelIndex &index)
{
    on_pbAdd_clicked();
}

void SelectMethodPage::on_lvSelectedMethods_doubleClicked(const QModelIndex &index)
{
    on_pbRemove_clicked();
}


void SelectMethodPage::on_pbDeleteAll_clicked()
{
    selectedMethods.clear();
    modelSelectedMethods->setStringList(selectedMethods);
    emit completeChanged();
}

void SelectMethodPage::on_pbAddFull_clicked()
{
    selectedMethods.append(modelMethods->stringList());
    modelSelectedMethods->setStringList(selectedMethods);
    emit completeChanged();
}

void SelectMethodPage::MethodDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem myOption(option);
    QString text = index.data().toString();
    text = text.left(text.indexOf('('));
    QItemDelegate::drawDisplay(painter, myOption, myOption.rect, text);
    QItemDelegate::drawFocus(painter, myOption, myOption.rect);
}
